package org.example.server.goods.service.goods.impl;

import jakarta.annotation.Resource;
import org.example.api.goods.goods.request.AddGoodsRequest;
import org.example.api.goods.goods.request.DeleteGoodsRequest;
import org.example.api.goods.goods.request.GoodsSearchRequest;
import org.example.frw.api.base.intf.ID;
import org.example.frw.verify.annotations.Verify;
import org.example.server.goods.mapstruct.goods.GoodsConverter;
import org.example.server.goods.repo.GoodsRepoInterface;
import org.example.server.goods.repo.doo.GoodsListItemDo;
import org.example.server.goods.service.goods.GoodsService;
import org.example.server.goods.service.goods.bo.GoodsFatDetailBo;
import org.example.server.goods.service.goods.bo.GoodsListItemBo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class GoodsServiceImpl implements GoodsService {
    @Resource
    private GoodsRepoInterface goodsRepo;
    @Resource
    private GoodsConverter goodsConverter;

    @Value("${minio.minio-endpoint:http://127.0.0.1:8000}")
    private String minioEndpoint;

    /**商品会通过canal同步到es
     * @param request request
     * @return id
     */
    @Override
    @Verify
    public ID addGoods(AddGoodsRequest request) {
        return goodsRepo.addGoods(request);
    }

    /**
     * @param id id
     * @return fatDetail
     */
    @Override
    public GoodsFatDetailBo fatDetail(Long id) {
        return goodsConverter.toGoodsFatDetailBo(goodsRepo.fatDetail(id));
    }

    @Override
    public List<GoodsListItemBo> goods(GoodsSearchRequest request) {
        List<GoodsListItemDo> goodsListItemDos = goodsRepo.pageGoods(request);
        goodsListItemDos.forEach(goodsListItemDo -> goodsListItemDo.setMainImage(minioEndpoint + goodsListItemDo.getMainImage()));
        return goodsConverter.toGoodsListItemBo(goodsListItemDos);
    }

    @Override
    public Long goodsCount(GoodsSearchRequest request) {
        return goodsRepo.goodsCount(request);
    }

    @Override
    public void deleteGoods(DeleteGoodsRequest request) {
        goodsRepo.deleteGoods(request);
    }
}
